package com.deer.wms.instock.service.enter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.instock.model.erpType.ErpType;
import com.deer.wms.instock.service.enter.EnterErp;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.erp.UpdatePPF;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import com.deer.wms.ware.task.service.SoDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: xu
 * @Date: 2022/08/15/13:27
 * @Description:
 */
@Component
public class Return251300022 implements EnterErp {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;
    @Autowired
    private SoDetailService soDetailService;

    @Override
    public ErpType gainType() {
        return    new ErpType(251300022, 24);
    }

    @Override
    public Map<String, String> EnterBeanAndPush(AsnMaster asnMaster, List<AsnDetail> asnDetails, BaseAssist enterAssist) {
        Set<Integer> sourceId = asnDetails.stream().map(AsnDetail::getSourceId).collect(Collectors.toSet());
        List<SoDetail> soDetails = soDetailService.list(new QueryWrapper<SoDetail>().in("erp_bill_id", sourceId));
        List<SoDetail> updateSoDetails = new ArrayList<>();
        List<UpdatePPF> list = new ArrayList<>();
        OrderToErpBO orderToErpBO = new OrderToErpBO();
        List<OrderDetailBO> details = new ArrayList<>();
        orderToErpBO.setBillTypeId(asnMaster.getAsnType());
        orderToErpBO.setSourceBillTypeId(asnMaster.getBillType());
        orderToErpBO.setBillDate(DateUtils.getNowDateString()); //日期
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("INPUT"));
        orderToErpBO.setRob(false);
        orderToErpBO.setCategoryId(ObjectUtil.isNotEmpty(enterAssist.getCategoryId()) ? enterAssist.getCategoryId() : 27718); //默认其他调拨
        orderToErpBO.setDeptId(enterAssist.getDeptId());
        for (AsnDetail asnDetail : asnDetails) {
            UpdatePPF updatePPF = new UpdatePPF();
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setSourceBillId(asnDetail.getSourceId()); //源单id
            orderDetailBO.setSourceBillDetailRowIndex(asnDetail.getRowIndex()); //行号
            orderDetailBO.setBatchNo(asnDetail.getBatchName()); //批号
            orderDetailBO.setQty(asnDetail.getQty()); //数量
//            orderDetailBO.setAuxPropId(asnDetail.getUpperPackDetailId()); //辅助类型id  33469
            orderDetailBO.setStockId(asnDetail.getStockId()); //
            details.add(orderDetailBO);
        }
        orderToErpBO.setDetails(details);
        //批量修改数量
        soDetailService.updateBatchById(soDetails);
        //修改可分配数量

        //红字退库
        return pushErpService.pushIMBIll(orderToErpBO);
    }


    public SoDetail setSoQty(AsnDetail asnDetail, List<SoDetail> soDetails) {
        SoDetail soDetail = soDetails.stream().filter(item -> item.getErpBillId().equals(asnDetail.getSourceId()) && item.getBomSplit().equals(asnDetail.getRowIndex()))
                .findFirst().get();
        soDetail.setAllottedQuantity(soDetail.getAllottedQuantity() - asnDetail.getQty());
        soDetail.setPickQuantity(soDetail.getPickQuantity() - asnDetail.getQty());
        soDetail.setDeliveryQuantity(soDetail.getDeliveryQuantity() - asnDetail.getQty());
        soDetail.setReturnQty(soDetail.getReturnQty() - asnDetail.getQty());
        soDetail.setTotalReturnQty(soDetail.getTotalReturnQty() + asnDetail.getQty());
        return soDetail;
    }
}
